package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 接收文件的方法
     */
    @PostMapping("/upload")
    @ApiOperation("新增文件")
    public Result insertFile(MultipartFile file){
      log.info("文件路径为:{}",file);
        //开始上传文件 到阿里云
        try {
            //获取文件后缀名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名字
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            //设置名称
            String oss= UUID.randomUUID().toString()+substring;

            String upload = aliOssUtil.upload(file.getBytes(), oss);
            return Result.success(upload);
        } catch (IOException e) {
            log.error("文件上传失败{}",e);
//            e.printStackTrace();
        }
        return null;
    }
}
